package com.wanwu.channel.validate;

import com.wanwu.channel.common.enums.BusinessCodeEnum;
import com.wanwu.channel.common.enums.PayType;
import com.wanwu.channel.common.exception.SystemException;
import com.wanwu.channel.facade.dto.req.ChPrepayReqDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;

/**
 * @ClassName PrepayValidate
 * @Description 预下单校验
 * @Author husheng
 * @Date 2020-01-16 20:33
 */
public class PrepayValidate {

    public static void validate(ChPrepayReqDto reqDto) {
        if (0 == reqDto.getPayType()) {
            throw SystemException.newValidateInstance("payType");
        }
        if (StringUtils.isEmpty(reqDto.getSubject())) {
            throw SystemException.newValidateInstance("subject");
        }
        if (ObjectUtils.isEmpty(reqDto.getPayAmount()) || reqDto.getPayAmount().compareTo(BigDecimal.ZERO) <= 0) {
            throw SystemException.newValidateInstance("payAmount");
        }
        if (StringUtils.isEmpty(reqDto.getBizOrderNo())) {
            throw SystemException.newValidateInstance("bizOrderNo");
        }
        if (StringUtils.isEmpty(reqDto.getBizCode())) {
            reqDto.setBizCode(BusinessCodeEnum.WANLIMU.getBizCode());
           // throw SystemException.newValidateInstance("bizCode 不能为空");
        }
        if (StringUtils.isEmpty(reqDto.getFrontUrl())) {
            throw SystemException.newValidateInstance("frontUrl");
        }
        if (StringUtils.isEmpty(reqDto.getCurrency())) {
            throw SystemException.newValidateInstance("currency");
        }
        //h5校验
        if (PayType.WECHAT_H5.check(reqDto.getPayType())) {
            if (StringUtils.isEmpty(reqDto.getSpbillCreateIp())) {
                throw SystemException.newValidateInstance("spbillCreateIp");
            }
            if (StringUtils.isEmpty(reqDto.getSceneCode())) {
                throw SystemException.newValidateInstance("sceneCode");
            }
            if (StringUtils.isEmpty(reqDto.getSceneName())) {
                throw SystemException.newValidateInstance("sceneName");
            }
        }

    }
}
